package cn.mumzone.pos.ui.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;
import cn.mumzone.basic.base.BaseFragment;
import cn.mumzone.basic.util.AppDateUtil;
import cn.mumzone.basic.util.DisplayUtil;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.ExceptionEntity;
import cn.mumzone.pos.presenter.ExceptionReportPresenter;
import cn.mumzone.pos.presenter.contract.ExceptionReportContract;
import cn.mumzone.pos.ui.adapter.ADA_Exception;

/**
 * author:        yupeng
 * created on:    2019/7/11 19:03
 * description:   异常情况
 */
public class FRA_Exception extends BaseFragment<ExceptionReportPresenter> implements ExceptionReportContract.View {

    @BindView(R.id.tv_start_time)
    TextView tvStartTime;
    @BindView(R.id.ll_start_time_select)
    LinearLayout llStartTimeSelect;
    @BindView(R.id.tv_end_time)
    TextView tvEndTime;
    @BindView(R.id.ll_end_time_select)
    LinearLayout llEndTimeSelect;
    @BindView(R.id.tv_print)
    TextView tvPrint;
    @BindView(R.id.rv_exceptions)
    RecyclerView rvExceptions;
    @BindView(R.id.tv_data_empty)
    TextView tvDataEmpty;

    public static FRA_Exception newInstance() {
        return new FRA_Exception();
    }

    private TimePickerView startDatePickDialog;
    private TimePickerView endDatePickDialog;

    private ADA_Exception adapter;

    private String startDate = "";
    private String endDate = "";

    @Override
    protected void initEventAndData() {

        rvExceptions.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new ADA_Exception(mContext);
        rvExceptions.setHasFixedSize(true);
        rvExceptions.setNestedScrollingEnabled(false);
        rvExceptions.setAdapter(adapter);


        initDatePickDialog();

        tvStartTime.setText(AppDateUtil.getDateToString(System.currentTimeMillis(), AppDateUtil.YYYY_MM_DD1));
        tvEndTime.setText(AppDateUtil.getDateToString(System.currentTimeMillis(), AppDateUtil.YYYY_MM_DD1));

        startDate = AppDateUtil.getDateToString(AppDateUtil.getDayStartTime(null), AppDateUtil.YYYY_MM_DD_HH_MM);
        endDate = AppDateUtil.getDateToString(AppDateUtil.getDayEndTime(null), AppDateUtil.YYYY_MM_DD_HH_MM);

        refreshReport();

    }

    @Override
    protected void onUserVisible() {
        refreshReport();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            refreshReport();
        }
    }

    private void initDatePickDialog() {
        Calendar nowDate = Calendar.getInstance();
        int nowYear = nowDate.get(Calendar.YEAR);

        Calendar startDate = Calendar.getInstance();
        startDate.set(nowYear - 2, 0, 1);
        Calendar endDate = Calendar.getInstance();
        startDatePickDialog = new TimePickerBuilder(mContext, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                String endTimeStamp = AppDateUtil.date2TimeStamp(tvEndTime.getText().toString().trim(), AppDateUtil.YYYY_MM_DD1);
                String startTime = AppDateUtil.date2TimeStamp(AppDateUtil.getDateToString(date.getTime(), AppDateUtil.YYYY_MM_DD), AppDateUtil.YYYY_MM_DD);
                if (Long.valueOf(startTime) > Long.valueOf(endTimeStamp)) {
                    showError("起始日期不能大于截止日期");
                    String startTimeStamp = AppDateUtil.date2TimeStamp(tvStartTime.getText().toString().trim(), AppDateUtil.YYYY_MM_DD1);
                    Calendar startDate = Calendar.getInstance();
                    startDate.setTimeInMillis(Long.valueOf(startTimeStamp));
                    startDatePickDialog.setDate(startDate);
                    return;
                }
                startDatePickDialog.dismiss();
                tvStartTime.setText(AppDateUtil.getDateToString(date.getTime(), AppDateUtil.YYYY_MM_DD1));
                FRA_Exception.this.startDate = AppDateUtil.getDateToString(AppDateUtil.getDayStartTime(date), AppDateUtil.YYYY_MM_DD_HH_MM);
                refreshReport();
            }
        }).isDialog(true).isCyclic(false).setOutSideCancelable(true)
                .setType(new boolean[]{true, true, true, false, false, false})
                .setDate(nowDate)
                .setRangDate(startDate, endDate)
                .build();
        startDatePickDialog.getDialog().getWindow().setLayout(DisplayUtil.dip2px(mContext, 500), WindowManager.LayoutParams.WRAP_CONTENT);

        endDatePickDialog = new TimePickerBuilder(mContext, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                String startTimeStamp = AppDateUtil.date2TimeStamp(tvStartTime.getText().toString().trim(), AppDateUtil.YYYY_MM_DD1);
                String endTime = AppDateUtil.date2TimeStamp(AppDateUtil.getDateToString(date.getTime(), AppDateUtil.YYYY_MM_DD), AppDateUtil.YYYY_MM_DD);
                if (Long.valueOf(endTime) < Long.valueOf(startTimeStamp)) {
                    showError("截止日期不能小于起始日期");
                    String endTimeStamp = AppDateUtil.date2TimeStamp(tvEndTime.getText().toString().trim(), AppDateUtil.YYYY_MM_DD1);
                    Calendar endDate = Calendar.getInstance();
                    endDate.setTimeInMillis(Long.valueOf(endTimeStamp));
                    endDatePickDialog.setDate(endDate);
                    return;
                }
                endDatePickDialog.dismiss();
                tvEndTime.setText(AppDateUtil.getDateToString(date.getTime(), AppDateUtil.YYYY_MM_DD1));
                FRA_Exception.this.endDate = AppDateUtil.getDateToString(AppDateUtil.getDayEndTime(date), AppDateUtil.YYYY_MM_DD_HH_MM);
                refreshReport();
            }
        }).isDialog(true).isCyclic(false).setOutSideCancelable(true)
                .setType(new boolean[]{true, true, true, false, false, false})
                .setDate(nowDate)
                .setRangDate(startDate, endDate)
                .build();
        endDatePickDialog.getDialog().getWindow().setLayout(DisplayUtil.dip2px(mContext, 500), WindowManager.LayoutParams.WRAP_CONTENT);


    }

    private void refreshReport() {

        mPresenter.requestExceptionInfo(startDate, endDate);
    }

    @OnClick({R.id.ll_start_time_select, R.id.ll_end_time_select})
    void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_start_time_select:
                startDatePickDialog.show();
                break;

            case R.id.ll_end_time_select:
                endDatePickDialog.show();
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (startDatePickDialog != null) {
            startDatePickDialog.dismiss();
        }
        if (endDatePickDialog != null) {
            endDatePickDialog.dismiss();
        }
    }

    @Override
    protected int getLayout() {
        return R.layout.fra_exception;
    }

    @Override
    public void showExceptionInfo(ArrayList<ExceptionEntity> arrayList) {
        adapter.setData(arrayList);
        adapter.notifyDataSetChanged();
        tvDataEmpty.setVisibility(ListUtil.isListEmpty(arrayList) ? View.VISIBLE : View.GONE);
    }
}
